<template>
  <div class="box">
    <div class="userMessage_header">
      <van-icon name="setting-o" @click="changeSettingBool" />
      <div class="setting" v-if="settingBool">
        <p @click="goHome">退出登录</p>
        <p @click="goLogin">更换账号</p>
        <p @click="goSpecialMessage">关于数据</p>
      </div>
    </div>
    <div class="userMessage" @click="changeSettingBoolFalse">
      <div class="userMessage_box">
        <div class="userMessage_img">
          <img :src="userObj.uservia" alt="">
        </div>
        <div class="userMessage_left">
          <b v-if="userObj.usernickname === '用户昵称'">爱美用户</b>
          <b v-else>{{userObj.usernickname}}</b>
          <span>
            <i @click="toChangeMsg">完善信息</i>
            <van-icon name="arrow" />
          </span>
          <p>{{userObj.usersignature}}</p>
        </div>
      </div>
      <ul class="userMessage_kind">
        <li>
          <p>0</p>
          <p>日记数</p>
        </li>
        <li>
          <p>0</p>
          <p>关注数</p>
        </li>
        <li>
          <p>0</p>
          <p>粉丝数</p>
        </li>
        <li>
          <p>0</p>
          <p>赞与收藏</p>
        </li>
      </ul>
    </div>
    <ul class="userKind" @click="changeSettingBoolFalse">
      <li @click="goMyOrder">
        <img class="iconfont" src="@/assets/images/dingdan.jpg" alt="">
        <p>我的订单</p>
      </li>
      <li @click="goMyLike">
        <img class="iconfont" src="@/assets/images/dianzan.jpg" alt="">
        <p>我的点赞</p>
      </li>
      <li @click="goMyCollection">
        <img class="iconfont" src="@/assets/images/shoucang.jpg" alt="">
        <p>我的收藏</p>
      </li>
      <li @click="goMyComment">
        <img class="iconfont" src="@/assets/images/pinglun.jpg" alt="">
        <p>我的评论</p>
      </li>
    </ul>
    <Recommend @click="changeSettingBoolFalse" />
  </div>
</template>

<script>
import { getUserMessage } from '../../api'
import Recommend from './recommend.vue'
import Vue from 'vue'
import { Icon, Toast } from 'vant'
Vue.use(Icon)
Vue.use(Toast)
export default {
  data () {
    return {
      userObj: {},
      settingBool: false,
      loginState: localStorage.getItem('loginState_AM')
    }
  },
  mounted () {
    if (this.loginState === 'no' || this.loginState === null) {
      this.$router.replace('/login')
    } else {
      getUserMessage({
        username: localStorage.getItem('userTel_AM'),
        token: localStorage.getItem('token_AM')
      }).then(res => {
        if (res.data.code === '200') {
          this.userObj = res.data.data[0]
        } else if (res.data.code === '400') {
          Toast('您的身份认证已过期，请重新登录')
          localStorage.removeItem('userTel_AM')
          localStorage.setItem('loginState_AM', 'no')
          this.$router.replace('/login')
        }
      })
    }
  },
  components: {
    Recommend
  },
  methods: {
    toChangeMsg () {
      this.$router.push('/changeMsg')
    },
    changeSettingBool () {
      this.settingBool = !this.settingBool
    },
    goHome () {
      localStorage.setItem('isLogin_AM', 1)
      localStorage.setItem('loginState_AM', 'no')
      this.$router.replace('/home')
    },
    goLogin () {
      localStorage.removeItem('userTel_AM')
      localStorage.setItem('isLogin_AM', 0)
      localStorage.setItem('loginState_AM', 'no')
      localStorage.removeItem('token_AM')
      this.$router.replace('/login')
    },
    changeSettingBoolFalse () {
      this.settingBool = false
    },
    goMyOrder () {
      this.$router.push('/myOrder')
    },
    goMyLike () {
      this.$router.push('/myLike')
    },
    goMyCollection () {
      this.$router.push('/myCollection')
    },
    goMyComment () {
      this.$router.push('/myComment')
    },
    goSpecialMessage () {
      this.$router.push('/specialMessage')
    }
  }
}
</script>

<style lang="scss" scoped>
.userMessage_header {
  display: block;
  width: 100%;
  height: 0.46rem;
  position: relative;
  padding: 0 0.15rem;
}
.userMessage {
  width: 100%;
  height: auto;
  border-bottom: 0.05rem solid #f2f8f6;
  .userMessage_box {
    width: 100%;
    height: 0.88rem;
    padding: 0.1rem 0.15rem;
    .userMessage_img {
      float: left;
      width: 0.67rem;
      height: 0.67rem;
      margin-right: 0.3rem;
      border: 0.02rem solid #F7B450;
      border-radius: 50%;
      margin-left: 0.15rem;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .userMessage_left {
      float: left;
      b {
        display: block;
        font-size: 0.18rem;
        line-height: 0.3rem;
        color: #5c5c5c;
        font-weight: normal;
      }
      span {
        height: 0.12rem;
        width: 100%;
        display: block;
        font-size: 0.12rem;
        line-height: 0.12rem;
        color: #c0c1c5;
        i {
          font-weight: normal;
          float: left;
        }
        .van-icon-arrow {
          float: left;
          font-size: 0.12rem;
          line-height: 0.12rem;
        }
      }
      p {
        color: #939393;
        width: 100%;
        height: 0.2rem;
        line-height: 0.24rem;
        font-size: 0.14rem;
        margin-top: 0.05rem;
      }
    }
  }
  .userMessage_kind {
    width: 100%;
    height: 0.65rem;
    padding: 0.1rem 0.15rem;
    li {
      float: left;
      width: 25%;
      text-align: center;
      p:nth-of-type(1) {
        color: #555555;
        line-height: 0.2rem;
        font-size: 0.2rem;
        font-weight: bold;
      }
      p:nth-of-type(2) {
        color: #8d8d8d;
        line-height: 0.2rem;
        font-size: 0.12rem;
        margin-top: 0.05rem;
      }
    }
  }
}
.userKind {
  width: 100%;
  height: 0.92rem;
  padding: 0.18rem 0.15rem;
  border-bottom: 0.05rem solid #f2f8f6;
  margin-bottom: 0.2rem;
  li {
    width: 25%;
    height: 100%;
    float: left;
    text-align: center;
    .iconfont {
      width: 0.3rem;
      height: 0.3rem;
      display: block;
      margin: 0 auto;
    }
    p {
      height: 0.14rem;
      font-size: 0.12rem;
      line-height: 0.14rem;
      color: #969696;
      margin-top: 0.06rem;
    }
  }
}
.van-icon-setting-o {
  font-size: 0.2rem;
  color: #555555;
  padding: 0 0.04rem;
  line-height: 0.46rem;
  float: right;
}
.setting {
  width: 1rem;
  height: auto;
  position: absolute;
  bottom: -1.1rem;
  right: 0.1rem;
  background: #4c4c4c;
  border-radius: 0.05rem;
  color: #d6d6d6;
  text-align: center;
  font-size: 0.14rem;
  p {
    line-height: 0.36rem;
  }
  p:nth-of-type(1) {
    border-bottom: 1px solid #515151;
  }
}
</style>
